class SettingsController < ApplicationController

  def index
    @settings = Setting.paginate(:all, :order => 'name asc', :page => params[:page])
  end

  def new
    @setting = Setting.new
  end

  def popup_new
    @bucket = Bucket.find(params[:bucket_id])
    @setting = Setting.new
  end

  def popup_create
    @bucket = Bucket.find(params[:setting][:add_bucket])
    @setting = Setting.new(params[:setting])
    if @setting.save
      render :partial => 'buckets/settings_list', :locals => { :settings => @bucket.settings }
    else
      render :text => @setting.errors.full_messages.join(","), :status => 400
    end
  end
  
  def create
    @setting = Setting.new(params[:setting])
    if @setting.save
      flash[:notice] = 'Setting created'
      redirect_to "/"
    else
      render :action => 'new'
    end
  end

  def edit
    @setting = Setting.find(params[:id])
  end

  def update
    @setting = Setting.find(params[:id])
    if @setting.update_attributes(params[:setting])
      flash[:notice] = 'Setting updated'
      redirect_to "/"
    else
      render :action => 'edit'
    end
  end

  def destroy
    @setting = Setting.find(params[:id])
    Setting.destroy(params[:id])
    flash[:notice] = "Setting has been deleted"
    redirect_to "/"
  end

  def remove
    @bucket = Bucket.find(params[:bucket_id])
    @setting = Setting.find(params[:id])
    @setting.remove_from!(@bucket)
    flash[:notice] = "setting removed from bucket"
    redirect_to bucket_url(@bucket)
  end

end
